#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define vec vector<int>
#define y1 y_____hahahaha_____1

using namespace std;

typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;

int read(){int x = 0,sgn = 1;char ch = getchar();for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;for(;isdigit(ch);ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x * sgn;}

const int ML = 3e7 + 10;
const int N = 2e5+10;

int ch[ML][2],val[ML],ndn;
int rt[N],n,m,p[N],b[N];
vec trash;

int newn(){int x ; if(trash.size()) return x = trash.back(),trash.pop_back(),x;return ++ndn;}

void upd(int &x,int pos,int v,int l = 1,int r = n){
	if(!x) x = newn(); val[x] += v; if(l == r) return; int mid = l + r >> 1;
	if(pos <= mid) upd(ch[x][0],pos,v,l,mid); else upd(ch[x][1],pos,v,mid + 1,r);
	if(!val[x]) trash.pb(x),ch[x][0] = ch[x][1] = val[x] = 0,x = 0;
}

int query(int x,int xl,int xr,int l = 1,int r = n){
	if(!x) return 0; if(xl == l && xr == r) return val[x]; int mid = l + r >> 1;
	if(xr <= mid) return query(ch[x][0],xl,xr,l,mid); else if(xl > mid) return query(ch[x][1],xl,xr,mid + 1,r);
	else return query(ch[x][0],xl,mid,l,mid) + query(ch[x][1],mid + 1,xr,mid + 1,r);
}

int qt(int x,int xl,int xr){int res = 0; for(;x;x -= x & -x) res += query(rt[x],xl,xr); return res;}

void ad(int x,int num,int v){for(;x <= n;x += x & -x) upd(rt[x],num,v);}

int main(){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in;m = in; for(int i = 1,x;i <= n;i++) x = in,p[x] = i;
	for(int i = 1;i <= n;i++) b[i] = p[in];
	for(int i = 1;i <= n;i++) ad(i,b[i],1);
	for(int i = 1;i <= m;i++){
		int op = in;
		if(op == 1){int l1 = in,r1 = in,l2 = in,r2 = in; int res = qt(r2,l1,r1) - qt(l2 - 1,l1,r1); printf("%d\n",res);}
		else{int x = in,y = in; ad(x,b[x],-1); ad(y,b[y],-1); swap(b[x],b[y]); ad(x,b[x],1); ad(y,b[y],1);}
	}
	return 0;
}
